//
//  MHCHCitysModel.m
//  Go!
//
//  Created by rimi on 16/7/12.
//  Copyright © 2016年 MHCH. All rights reserved.
//

#import "MHCHCitysModel.h"
#import "NetWorkManager+HotelNetWorkManager.h"

@interface MHCHCitysModel ()
@property (nonatomic, assign) BOOL isonce;
@end

@implementation MHCHCitysModel


+(void)getTheCitys{
    NSArray *tempnameArray = [[NSUserDefaults standardUserDefaults] objectForKey:AllCitiesArray];
    NSMutableArray *citynameArray = [NSMutableArray array];
    NSMutableArray *cityidArray = [NSMutableArray array];
   
    NSMutableArray *allthingArray = [NSMutableArray array];
    
    NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *path = [docPath stringByAppendingPathComponent:@"citymeesage"];
    NSArray *array = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
    if (array == nil) {
        for (int i = 0; i<tempnameArray.count; i++) {
            NSString *citynamestr = tempnameArray[i][@"city_name"];
            NSString *cityidstr = tempnameArray[i][@"city_id"];

            [citynameArray addObject:citynamestr];
            [cityidArray addObject:cityidstr];
        
        [NetWorkManager pickViewChoisCityWithCityID:cityidstr successcity:^(id responseObject) {
            NSMutableArray *districtsArray = [NSMutableArray array];
            NSArray *temparray = responseObject[@"districts"];
            for (int j = 0; j < temparray.count; j++) {
                NSDictionary *dict = @{@"district_name":responseObject[@"districts"][j][@"district_name"],@"district_id":responseObject[@"districts"][j][@"district_id"]};
                [districtsArray addObject:dict];
                
            }
            
            NSDictionary *dict2 = @{@"cityname":citynamestr,@"cityid":cityidstr,@"districts":districtsArray};
            
            [allthingArray addObject:dict2];
            if (i == tempnameArray.count - 1) {
                [MHCHCitysModel setTheMessageAboutCity:allthingArray];
                
            }
        } failurecity:^(NSError *error) {
            
        }];
        }
    }
}

+(void)setTheMessageAboutCity:(NSArray *)citymessage{
    //获取文件路径
    NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *path = [docPath stringByAppendingPathComponent:@"citymeesage"];
    //将对象保存到文件中
    [NSKeyedArchiver archiveRootObject:citymessage toFile:path];
}
static NSArray *array = nil;
+(NSArray *)getTheCityMessage{
    if (array == nil) {
        NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
        NSString *path = [docPath stringByAppendingPathComponent:@"citymeesage"];
        array = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
    }
    return array;
}




@end
